package com.example.learn;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private AppCompatImageView backImageView = null;
    private AppCompatImageView goImageView = null;
    private AppCompatImageView refreshImageView = null;
    private AppCompatImageView sendImageView = null;
    private AppCompatEditText linkEditText = null;
    private SwipeRefreshLayout mainSwipeRefreshLayout = null;
    private WebView mainWebView = null;
    private ContentLoadingProgressBar mainProgressBar = null;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        backImageView = findViewById(R.id.backImageView);
        goImageView = findViewById(R.id.goImageView);
        refreshImageView = findViewById(R.id.refreshImageView);
        sendImageView = findViewById(R.id.sendImageView);
        linkEditText = findViewById(R.id.linkEditText);
        mainSwipeRefreshLayout = findViewById(R.id.mainSwipeRefreshLayout);
        mainWebView = findViewById(R.id.mainWebView);
        mainProgressBar = findViewById(R.id.mainProgressBar);

        mainSwipeRefreshLayout.setColorSchemeColors(
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorPrimary)
        );

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mainWebView.loadUrl(Objects.requireNonNull(linkEditText.getText()).toString());

        mainWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mainWebView.loadUrl(url);
                return true;
            }
        });

        mainWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                mainProgressBar.setVisibility(View.VISIBLE);
                mainProgressBar.setProgress(newProgress);
                if (newProgress == 100) {
                    mainProgressBar.setVisibility(View.GONE);
                }
            }
        });

        backImageView.setOnClickListener(v -> {
            mainWebView.goBack();//后退
        });

        goImageView.setOnClickListener(v -> {
            mainWebView.goForward();//前进
        });

        refreshImageView.setOnClickListener(v -> {
            mainWebView.reload();//重新加载
        });

        sendImageView.setOnClickListener(v -> mainWebView.loadUrl(Objects.requireNonNull(linkEditText.getText()).toString()));

        mainSwipeRefreshLayout.setOnRefreshListener(() ->
                new Handler().postDelayed(() -> {
                    mainSwipeRefreshLayout.setRefreshing(false);
                    mainWebView.reload();
                }, 3000)
        );

    }

}
